function loadXMLDoc() {
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var json = JSON.parse(xmlhttp.responseText);
            console.log(json);
            for (var i = 0; i < json.data.length; i++) {
                //创建li标签
                var li = document.createElement("li");
                //创建span标签
                var spanName = document.createElement("span");
                spanName.innerHTML = json.data[i].studentName;
                var spanSex = document.createElement("span");
                spanSex.innerHTML = json.data[i].sex;
                var spanAge = document.createElement("span");
                spanAge.innerHTML = json.data[i].age;
                //把span内容加到li里面
                li.appendChild(spanName);
                li.appendChild(spanSex);
                li.appendChild(spanAge);
                //把li标签的内容加到ul里面
                document.querySelector("#list").appendChild(li);
            }
        }
    };
    xmlhttp.open("GET", "http://localhost/ajaxApi/public/index.php/index/student/student", true);
    xmlhttp.send();
}
loadXMLDoc();


    document.querySelector("#btn").addEventListener("click", function () {
        //获取姓名
        var x = document.querySelector("#x").value;
        console.log(x);
        //获取性别
        var n = document.querySelector("#n").value;
        console.log(n);

        //获取年龄
        var b = document.querySelector("#b").value;
        console.log(b);


        //ajax代码

        var e;
        e = new XMLHttpRequest();
        e.onreadystatechange = function () {
            if (e.readyState == 4 && e.status == 200) {
                document.querySelector("#list").innerHTML = "";
                loadXMLDoc();
            }
        };
        e.open("GET", "http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+x+"&sex="+n+"&age="+b, true);
        e.send();
    });





